package com.lee.controller;

import com.github.pagehelper.PageInfo;
import com.lee.bean.Follow;
import com.lee.bean.Refer;
import com.lee.service.ReferService;
import com.lee.util.AjaxResult;
import com.lee.util.MD5Util;
import com.lee.util.TimeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/4/17.
 */
@Controller
public class ReferController {

    @Resource
    private ReferService referService;

    @RequestMapping(value = "referlist")
    public String referList() {
        return "refer/listRefer";
    }

    @RequestMapping(value = "showRefer")
    public String showRefer() {
        return "refer/showRefer";
    }

    @RequestMapping(value = "editRefer")
    public String editRefer() {
        return "refer/editRefer";
    }

    @RequestMapping(value = "addStudent")
    public String addStudent() {
        return "refer/addStudent";
    }

    @RequestMapping(value = "referpage")
    public ModelAndView referByPage(@RequestParam(value = "pageNo", required = false) Integer pageNo,//当前页
                                @RequestParam("status") String status,
                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("referlist");
        modelAndView.addObject(referService.queryReferByPage(pageNo, 3,status));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "selectrefer")
    public AjaxResult selectRefer(@RequestParam("rid") String rid) {
        return referService.selectRefer(rid);
    }

    @ResponseBody
    @RequestMapping("selectallfollow")
    public AjaxResult selectFollow(@RequestParam("referid")String referid) {
        return referService.selectAllFollow(referid);
    }

    @RequestMapping(value = "addfollowpage")
    public String addFollowPage() {
        return "follow/addOrEditFollow";
    }

    @ResponseBody
    @RequestMapping(value = "addfollow")
    public AjaxResult addFollow(@RequestParam("content") String content, @RequestParam("referid") String referid, @RequestParam("staffid") String staffid) {
        AjaxResult result = new AjaxResult();
        if (content == null) {
            result.setCode("0");
            result.setMessage("请填写内容");
            return result;
        }
        Follow follow = new Follow(content, staffid, referid);
        String time = TimeUtil.time();
        follow.setFollowtime(time);
        follow.setFollowid(MD5Util.changeStr(time + content));
        return referService.addFollow(follow);
    }

    @ResponseBody
    @RequestMapping(value = "doEditRefer")
    public AjaxResult doEditRefer(@RequestParam(value = "name",required = false) String name,
                                  @RequestParam(value = "tel",required = false) String tel,
                                  @RequestParam(value = "referid",required = false) String referid,
                                  @RequestParam(value = "qq",required = false) String qq,
                                  @RequestParam(value = "level",required = false) String level,
                                  @RequestParam(value = "type",required = false) String type,
                                  @RequestParam(value = "source",required = false) String source,
                                  @RequestParam(value = "classid",required = false) String classid,
                                  @RequestParam(value = "remark",required = false) String remark) {
        Refer refer = new Refer(referid,name,tel,qq,level,classid,type,source,remark);
        System.out.println(refer);
        return referService.editRefer(refer);
    }
}
